package cn.dengta.webapp.trade.dao;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import cn.dengta.common.web.Page;
import cn.dengta.webapp.base.dao.BaseDao;
import cn.dengta.webapp.trade.dto.UserCoinDTO;
import cn.dengta.webapp.trade.entity.UserCoin;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.cursor.Cursor;
import org.apache.ibatis.mapping.ResultSetType;

/**
 * 用户钻石 Mapper 接口
 */
public interface UserCoinDao extends BaseDao<UserCoin> {

	@Select("SELECT log.*, u.id uId, u.nickname, u.avatar, u.gender, u.agent_id agentId FROM tr_user_coin log JOIN dt_user u ON log.user_id = u.id ${ew.customSqlSegment}")
	Page<UserCoinDTO> findPage(Page<UserCoin> page, @Param("ew") QueryWrapper<UserCoin> wrapper);

	@Update("UPDATE tr_user_coin SET all_coin_recharge = (all_coin_recharge + #{recharge}) WHERE id = #{id} AND #{recharge} > 0")
	int updateAllCoinRecharge(@Param("id") Long id, @Param("recharge") BigDecimal recharge);

	/**
	 * 查询最小更新时间
	 */
	@Options(resultSetType = ResultSetType.FORWARD_ONLY, fetchSize = 1000)
	@ResultType(UserCoin.class)
	@Select("SELECT * FROM tr_user_coin log" +
			" WHERE update_time >= #{minUpdateTime}" +
			" ORDER BY id DESC")
	Cursor<UserCoin> scanMinUpdateTimeCoin(@Param("minUpdateTime") Date minUpdateTime);

	@Select("SELECT user_id FROM tr_user_coin WHERE total_recharge >= #{amount}")
	List<Long> findGeTotalRecharge(@Param("amount") Integer amount);

	@Select("SELECT user_id FROM tr_user_coin WHERE total_recharge BETWEEN #{beginAmount} AND #{endAmount}")
	List<Long> findIntervalTotalRecharge(@Param("beginAmount") Integer beginAmount, @Param("endAmount") Integer endAmount);

}
